package view.book;

import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.Date;

import javax.swing.*;

import entity.Book;
import entity.Borrow;
import entity.User;
import service.book.BorrowServer;
import service.book.BorrowServerImpl;
import utils.ComonUtil;
import view.MainView;

import view.CommonView.CommonBookInfoView;
import view.CommonView.CommonHrJLabel;
import view.CommonView.CommonJPanel;
import view.CommonView.CommonUserInfoView;

/**
 * @Author: Lin Hongwei
 * @LastEditors: Lin Hongwei
 * @email: 1365755396@qq.com
 * @CSDN: https://blog.csdn.net/weixin_45429409
 * @Description: 图书借阅模块
 *
 */
public class BorrowBookView extends CommonJPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private final static int MAX_BORROW = 20;

	// 创建搜索按钮
	JButton searchButton;

	// 创建确认借阅按钮
	JButton borrowButton;
	// 图书编号,用户编号（查询）
	JTextField bookIdField,userIdField;
	
	
	
	JLabel numTxtJLabel,okTxtJLabel;
	
	BorrowServer borrowServer = new BorrowServerImpl();
	
	
	//当前时间
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
    String nowTime =  df.format(new Date());
	

	public BorrowBookView() {		
		super(TYPE_TRANSPARENT);
		setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));

		// 标签、输入框面板
		JPanel searchPanel = new CommonJPanel(TYPE_TRANSPARENT);
		searchPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 20));
		searchPanel.setPreferredSize(new Dimension(MainView.instance.getWidth(), 50));
		add(searchPanel);

		// 创建图书名称标签
		JLabel labelBookId = new JLabel("图书编号：");
		searchPanel.add(labelBookId);
		// 创建图书Id输入框
		bookIdField = new JTextField();
		bookIdField.setPreferredSize(new Dimension(120, 30));
		searchPanel.add(bookIdField);

		// 创建读者编号
		JLabel labelUser = new JLabel("读者编号：");
		searchPanel.add(labelUser);
		// 创建读者编号
		userIdField = new JTextField();
		userIdField.setPreferredSize(new Dimension(120, 30));
		searchPanel.add(userIdField);

		// 创建按钮
		searchButton = new JButton("查询");
		searchButton.setPreferredSize(new Dimension(100, 30));
		searchPanel.add(searchButton);

		// 分割线
		JLabel hrBook = new CommonHrJLabel("— — — — — — 图书信息 — — — — — —");
		add(hrBook);

		// 图书信息内容
		JPanel mainPanel = new CommonBookInfoView();
		add(mainPanel);

		// 分割
		JLabel hrBook1 = new CommonHrJLabel("— — — — — — 读者信息 — — — — — —");
		add(hrBook1);

		// 用戶信息内容
		JPanel userPanel = new CommonUserInfoView();
		add(userPanel);
		
		
		
		// 分割
		JLabel hrBook2 = new CommonHrJLabel("— — — — — — 借阅信息 — — — — — —");
		add(hrBook2);
		
		//
		JLabel labelNum = new JLabel("该读者已经借图书数量：");
		labelNum.setPreferredSize(new Dimension(200, 20));
		add(labelNum);
		numTxtJLabel = new JLabel("18");
		numTxtJLabel.setPreferredSize(new Dimension((MainView.instance.getWidth()-300), 20));
		numTxtJLabel.setForeground(Color.RED);
		add(numTxtJLabel);
		//
		JLabel labelOk = new JLabel("该读者是否可以借该图书：");
		labelOk.setPreferredSize(new Dimension(200, 20));
		add(labelOk);
		okTxtJLabel = new JLabel("是");
		okTxtJLabel.setPreferredSize(new Dimension((MainView.instance.getWidth()-300), 20));
		okTxtJLabel.setForeground(Color.RED);
		add(okTxtJLabel);
		//
		JLabel labelTime = new JLabel("借阅时间：");
		labelTime.setPreferredSize(new Dimension(200, 20));
		add(labelTime);
		JLabel timeTxtJLabel = new JLabel(nowTime);
		timeTxtJLabel.setPreferredSize(new Dimension((MainView.instance.getWidth()-300), 20));
		timeTxtJLabel.setForeground(Color.RED);
		add(timeTxtJLabel);
		

		// 创建按钮面板
		JPanel borrowPanel = new CommonJPanel(TYPE_TRANSPARENT);
		add(borrowPanel);

		// 创建确认借阅按钮
		borrowButton = new JButton("确认借阅");
		borrowButton.setPreferredSize(new Dimension(180, 40));
		borrowPanel.add(borrowButton);

		// 添加搜索按钮事件
		addSearchListener();

		// 添加确认按钮事件
		addBorrowListener();
		
		
		resetInput(true);//初始化
		
		

	}

	/**
	 * 获取表格选中当前行的数据
	 * 
	 * @return
	 */
//    public Book getSelectBook() {
//        int index = table.getSelectedRow();
//        return BookTableModel.cs.get(index);
//    }
	public void getSelectBook() {
	}

	/**
	 * 添加搜索按钮事件
	 */
	public void addSearchListener() {
		searchButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				
		        if(ComonUtil.isEmpty(bookIdField.getText())||ComonUtil.isEmpty(userIdField.getText())) {
		        	JOptionPane.showMessageDialog(BorrowBookView.this, "用户id和图书id不能为空！");
					return;
		        }
				
		        searchData();
				
//				Borrow borrow = new Borrow();
//				borrow.setFlag(1);
//				int bookId = Integer.parseInt(bookIdField.getText());
//				borrow.setBookId(bookId);
//				Book book = new Book();
//				book.setId(bookId);
//				borrow.setBook(book);
//				borrow.setUserId(userIdField.getText());
//				User user = new User();
//				user.setId(userIdField.getText());
//				borrow.setUser(user);
//				
//				
//				Borrow infoBorrow = borrowServer.getBorrowInfo(borrow);
//				updateData(infoBorrow);
				

			}
		});
	}

	/**
	 * 添加确认借阅按钮事件
	 */
	public void addBorrowListener() {
		borrowButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(okTxtJLabel.getText().equals("否")) {
					JOptionPane.showMessageDialog(BorrowBookView.this, "当前不可借阅！");
					return;
				}
				
				Borrow borrow = new Borrow();
				borrow.setFlag(1);
				int bookId = Integer.parseInt(bookIdField.getText());
				borrow.setBookId(bookId);
				Book book = new Book();
				book.setId(bookId);
				borrow.setBook(book);
				borrow.setUserId(userIdField.getText());
				User user = new User();
				user.setId(userIdField.getText());
				borrow.setUser(user);
				nowTime =  df.format(new Date());
				borrow.setBorrowTime(nowTime);
				
				String flag = "借阅失败";
				if(borrowServer.addBorrow(borrow)) {
					flag = "借阅成功";
					searchData();
				}
				JOptionPane.showMessageDialog(BorrowBookView.this, flag);
			
				
				

				
			}
		});
	}
	
	/**
	 * @查询数据
	 */
	public void searchData() {
		Borrow borrow = new Borrow();
		borrow.setFlag(1);
		int bookId = Integer.parseInt(bookIdField.getText());
//		borrow.setBookId(bookId);
		Book book = new Book();
		book.setId(bookId);
		borrow.setBook(book);
		borrow.setUserId(userIdField.getText());
		User user = new User();
		user.setId(userIdField.getText());
		borrow.setUser(user);
		
		
		Borrow infoBorrow = borrowServer.getBorrowInfo(borrow);
		updateData(infoBorrow);
	}

	/**
	 * 更新数据
	 */
	public void updateData(Borrow borrow) {
		resetInput(false);
		setBookInfo(borrow);
		setUserInfo(borrow);
	}
	
	/**
	 *  @ 设置图书数据
	 */
	public void setBookInfo(Borrow borrow) {
		Book book = borrow.getBook();
		if(book==null) {
			okTxtJLabel.setText("否");
			return;
		}
		System.out.println(book.getBookName());
		CommonBookInfoView.bookNameTxt.setText(book.getBookName());
		CommonBookInfoView.bookAuthorTxt.setText(book.getBookAuthor());
		CommonBookInfoView.bookPressTxt.setText(book.getBookPress());
		CommonBookInfoView.bookPressTimeTxt.setText(book.getBookPressTime());
		CommonBookInfoView.bookPrriceTxt.setText(book.getBookPrice());
		int borrowingNumbers = book.getBorrowingNumbers();
		CommonBookInfoView.bookStockTxt.setText(String.valueOf(borrowingNumbers));
		
		String isFlag = borrowingNumbers<=0?"否":"是";//库存
		okTxtJLabel.setText(isFlag);
		
		
	}
	/**
	 * @ 设置用户信息
	 */
	public void setUserInfo(Borrow borrow) {
		User user = borrow.getUser();
		Book book = borrow.getBook();
		if(user==null) {
			okTxtJLabel.setText("否");
			return;
		}
		System.out.println(user.getUserName());
		
		int num = MAX_BORROW - borrow.getCount();//该人可借数

		if(num<=0) {
			num=0;
			okTxtJLabel.setText("否");
		}
		if(num>0&&book!=null) {
			int borrowingNumbers = book.getBorrowingNumbers();//库存
			String isFlag = borrowingNumbers<=0?"否":"是";//库存
			okTxtJLabel.setText(isFlag);
		}

		CommonUserInfoView.userNameTxt.setText(user.getUserName());
		CommonUserInfoView.userTypeTxt.setText(user.getType());
		CommonUserInfoView.numTxt.setText(String.valueOf(num));
//		CommonUserInfoView.bookPressTimeTxt.setText("— —");
		numTxtJLabel.setText(String.valueOf(borrow.getCount()));
		
	}
	/**
	 * @ 重置
	 */
	public void resetInput(boolean isNoSearch) {
		if(isNoSearch) {
			bookIdField.setText("");
		    userIdField.setText("");
		}
		
		okTxtJLabel.setText("否");
		numTxtJLabel.setText("— —");
        // numTxtJLabel.setText(String.valueOf(MAX_BORROW));
		CommonBookInfoView.bookNameTxt.setText("— —");
		CommonBookInfoView.bookAuthorTxt.setText("— —");
		CommonBookInfoView.bookPressTxt.setText("— —");
		CommonBookInfoView.bookPressTimeTxt.setText("— —");
		CommonBookInfoView.bookPrriceTxt.setText("— —");
		CommonBookInfoView.bookStockTxt.setText("— —");
		

		CommonUserInfoView.userNameTxt.setText("— —");
		CommonUserInfoView.userTypeTxt.setText("— —");
		CommonUserInfoView.numTxt.setText(String.valueOf(MAX_BORROW));
        //CommonUserInfoView.bookPressTimeTxt.setText("— —");

	
	}
}
